package com.codeavatar.catools.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public abstract class CaRecyclerViewAdapter extends RecyclerView.Adapter<CaRecyclerViewAdapter.RvViewHolder> {

    private final String TAG = this.getClass().getSimpleName();

    //++++++++++++++++++++++++++++++++++++++
    //++ 抽象方法
    //++++++++++++++++++++++++++++++++++++++

    public abstract void bindView(RvViewHolder holder, int position);
    public abstract void bindClick(View v,int position);
    public abstract void bindChildClick(View v,int position);
    public abstract int getCount();
    public abstract int getResId();
    public abstract Context getContext();
    public abstract boolean getClickable();

    //++++++++++++++++++++++++++++++++++++++
    //++ 重写方法
    //++++++++++++++++++++++++++++++++++++++

    @NonNull
    @Override
    public RvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layview = LayoutInflater.from(parent.getContext()).inflate(this.getResId(), parent,false);
        return new RvViewHolder(layview);
    }

    @Override
    public void onBindViewHolder(@NonNull RvViewHolder holder, int position) {
        if(this.getClickable()){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bindClick(v,holder.getAdapterPosition());
                }
            });
        }
        this.bindView(holder, position);
    }

    @Override
    public int getItemCount() {
        return this.getCount();
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部类或接口
    //++++++++++++++++++++++++++++++++++++++

    //视图处理器
    public class RvViewHolder extends RecyclerView.ViewHolder{
        private View layview;
        private SparseArrayCompat<View> views;

        public RvViewHolder(@NonNull View itemView) {
            super(itemView);
            this.layview = itemView;
            this.views = new SparseArrayCompat<>();
        }

        public <T extends View> T getView(int viewId)
        {
            View view = views.get(viewId);
            if (view == null)
            {
                view = layview.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }

        /**
         * 以下可以自由扩展
         */
        public RvViewHolder setTextView(int viewId, String text)
        {
            TextView tv = getView(viewId);
            tv.setText(text);
            return this;
        }

        public RvViewHolder setImageView(int viewId, int resId)
        {
            ImageView view = getView(viewId);
            view.setImageResource(resId);
            return this;
        }

        public RvViewHolder setOnClickListener(int ...viewId)
        {
            for(int item:viewId){
                View view = getView(item);
                final int position = super.getAdapterPosition();
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bindChildClick(v,position);
                    }
                });
            }
            return this;
        }
    };
}
